//
//  ForgetPassWord.m
//  HappyStore
//
//  Created by 点点充 on 16/1/12.
//  Copyright © 2016年 LLJ. All rights reserved.
//

#import "ForgetPassWord.h"

@interface ForgetPassWord ()

@end

@implementation ForgetPassWord

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    self.navigationController.navigationBarHidden = NO;
    
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 190, 44)];
    titleLabel.backgroundColor = [UIColor clearColor];
    titleLabel.font = [UIFont systemFontOfSize:19];
    if (self.titleStr) {
        titleLabel.text = self.titleStr;
    }else{
        titleLabel.text = @"找回密码";
    }
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    self.navigationItem.titleView = titleLabel;
    
    //设置返回按钮
    UIBarButtonItem *leftItem = [UIBarButtonItem itemWithTarget:self action:@selector(leftBarButtonItemClick) image:@"Back" highImage:@"Back"];
    self.navigationItem.leftBarButtonItem = leftItem;
    
    //开启侧滑
    self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;
    
}

-(void)leftBarButtonItemClick{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


//发送验证码
- (IBAction)btnClickPostYZM:(UIButton *)sender
{
    BOOL isSuccess = YES;
    isSuccess = [_tfUserPhoneNum.text isValidPhoneNum];
    if (!isSuccess) {

        [self showSingleIconBubble:@"输入的手机号码有误"];

    }
    
    if (isSuccess) {
        //zc-yzm1-normal
        
        //1	获取验证码	http://cxs:8080/lensum/user_getCode_app_user.im	post	toCode:电话号码	{"result":"1","msg":"验证码发送手机成功!30分钟内有效!","data":"315925"}	{"result":"2","msg":"获取验证码失败，请仔细查看输入的接收端是否有误!","data":""}

//        [MRProgressOverlayView show];

        [self showRoundProgressWithTitle:@"请稍等..."];

        NSMutableDictionary *muDicParameters = [NSMutableDictionary dictionary];
        [muDicParameters setObject:_tfUserPhoneNum.text forKey:@"toCode"];//电话号码
        
        
        NSString *strURl = [NSString stringWithFormat:@"%@%@",URL_DEFINE,@"user_getCode_app_user.im"];
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        [manager POST:strURl parameters:muDicParameters progress:^(NSProgress * _Nonnull uploadProgress) {
            
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            
            NSDictionary *dicInfo = [NSDictionary dictionaryWithDictionary:responseObject];
//            NSLog(@"%@",dicInfo);

            if ([[dicInfo objectForKey:@"result"] integerValue] == 1) {

                [self showRightWithTitle:[dicInfo objectForKey:@"msg"] autoCloseTime:2];
                [self startTime];
            }else {

                [self showSingleIconBubble:[dicInfo objectForKey:@"msg"]];
            }
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

            [self showSingleIconBubble:error.localizedDescription];

        }];
    }
    
    
    
    
}


- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    if (textField == _tfPassword) {
        _imgV_Password.image = [UIImage imageNamed:@"zc-bk-selected"];
    }
    
    if (textField == _tfUserPhoneNum) {
        _imgV_UserPhoneNum.image = [UIImage imageNamed:@"zc-bk-selected"];
    }
    
    if (textField == _tfYZM) {
        _imgV_YZM.image = [UIImage imageNamed:@"zc-bk-selected"];
    }
    
    
    return YES;
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    if (textField == _tfPassword) {
        _imgV_Password.image = [UIImage imageNamed:@"zc-bk-normal"];
    }
    
    if (textField == _tfUserPhoneNum) {
        _imgV_UserPhoneNum.image = [UIImage imageNamed:@"zc-bk-normal"];
    }
    
    if (textField == _tfYZM) {
        _imgV_YZM.image = [UIImage imageNamed:@"zc-bk-normal"];
    }
}

-(void)startTime{
    __block int timeout = 30; //倒计时时间
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    dispatch_source_set_event_handler(_timer, ^{
        if(timeout<=0){ //倒计时结束，关闭
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                [_btnYZM setBackgroundImage:[UIImage imageNamed:@"zc-yzm1-normal"] forState:UIControlStateNormal];
                [_btnYZM setTitle:@"" forState:UIControlStateNormal];
                _btnYZM.userInteractionEnabled = YES;
            });
        }else{
            int seconds = timeout % 60;
            NSString *strTime = [NSString stringWithFormat:@"%.2d", seconds];
            [_btnYZM setBackgroundImage:[UIImage imageNamed:@"zc-yzm1-selected"] forState:UIControlStateNormal];
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                //NSLog(@"____%@",strTime);
                [UIView beginAnimations:nil context:nil];
                [UIView setAnimationDuration:1];
                
                [_btnYZM setTitle:[NSString stringWithFormat:@"%@s",strTime] forState:UIControlStateNormal];
                [UIView commitAnimations];
                _btnYZM.userInteractionEnabled = NO;
            });
            timeout--;
        }
    });
    dispatch_resume(_timer);
}



- (IBAction)btnClickNext:(id)sender
{
    BOOL isSuccess = YES;
    isSuccess = [_tfUserPhoneNum.text isValidPhoneNum];
    if (!isSuccess) {

        [self showSingleIconBubble:@"输入的手机号码有误"];

    }
    
    
    if (isSuccess == YES) {
        if (_tfYZM.text.length != 6) {
            isSuccess = NO;
            [self showSingleIconBubble:@"验证码长度不等于6位"];
        }
    }
    
    if (isSuccess == YES) {
        if (_tfPassword.text.length < 6) {
            isSuccess = NO;

            [self showSingleIconBubble:@"密码小于6位"];

        }
    }
    
    if (isSuccess) {
        //code=验证码&phone=手机号码&newPassword=新密码
        [self showRoundProgressWithTitle:@"请稍后..."];
        NSMutableDictionary *muDicParameters = [NSMutableDictionary dictionary];
        [muDicParameters setObject:_tfYZM.text forKey:@"code"];//验证码
        [muDicParameters setObject:_tfUserPhoneNum.text forKey:@"phone"];//手机号码
        [muDicParameters setObject:allConstMemberToken forKey:@"token"];
        
        NSString *strURl = @"";
        if ([self.titleStr isEqualToString:@"修改支付密码"]) {
            [muDicParameters setObject:_tfPassword.text forKey:@"newPayPass"];//支付密码
            strURl = [NSString stringWithFormat:@"%@%@",URL_DEFINE,@"resetPayPass_app_pay.im.lensum"];
        }else{
            [muDicParameters setObject:_tfPassword.text forKey:@"newPassword"];//密码
             strURl = [NSString stringWithFormat:@"%@%@",URL_DEFINE,@"resetPassword_app_user.im"];
        }
       
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        [manager POST:strURl parameters:muDicParameters progress:^(NSProgress * _Nonnull uploadProgress) {
            
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            
            NSDictionary *dicInfo = [NSDictionary dictionaryWithDictionary:responseObject];
//            NSLog(@"%@",dicInfo);

            if ([[dicInfo objectForKey:@"result"] integerValue] == 1) {

                [self showRightWithTitle:[dicInfo objectForKey:@"msg"] autoCloseTime:2];
                [self performSelector:@selector(btnClickDismiss) withObject:nil afterDelay:kHideHudDelay];
            }else{
                [self showSingleIconBubble:dicInfo[@"msg"]];

            }

        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

           [self showSingleIconBubble:error.localizedDescription];

        }];
    }
    
}

- (void)btnClickDismiss
{
    [self.navigationController popViewControllerAnimated:YES];
}



@end
